package com.xijia.wuyouwallet.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.alibaba.fastjson.JSONObject;
import com.xijia.wuyouwallet.MyApp;
import com.xijia.wuyouwallet.R;
import com.xijia.wuyouwallet.api.MyCallBack;
import com.xijia.wuyouwallet.base.BaseActivity;
import com.xijia.wuyouwallet.utils.Constants;
import com.xijia.wuyouwallet.utils.MyLogger;
import com.xijia.wuyouwallet.utils.UrlConfig;
import com.xijia.wuyouwallet.utils.XUtil;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/31 0031.
 */

public class RelieveAcountActivity extends BaseActivity implements View.OnClickListener{

    private ImageView relieve_acount_back;

    private TextView relieve_acount_title,relieve_acount_phone_tv;

    private EditText relieve_acount_code_et;

    private TextView relieve_acount_code_tv,relieve_acount_config;

    private String phoneNum,type;

    private int countSeconds = 60;// 倒数秒数

    private Handler hanlder = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    if (countSeconds > 0) {
                        --countSeconds;
                        relieve_acount_code_tv.setText(countSeconds + "s");
                        hanlder.sendEmptyMessageDelayed(0, 1000);
                    } else {
                        countSeconds = 60;
                        relieve_acount_code_tv.setText("重新获取");
                        relieve_acount_code_tv.setClickable(true);
                    }
                    break;
            }
        }
    };
    // 开始倒计时
    private void startCountBack() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                relieve_acount_code_tv.setText(countSeconds + "s");
                hanlder.sendEmptyMessage(0);
                relieve_acount_code_tv.setClickable(false);
            }
        });
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_relieve_acount);
        initView();
    }

    private void initView(){
        relieve_acount_back = (ImageView) findViewById(R.id.relieve_acount_back);
        relieve_acount_back.setOnClickListener(this);
        relieve_acount_title = (TextView) findViewById(R.id.relieve_acount_title);
        relieve_acount_phone_tv = (TextView) findViewById(R.id.relieve_acount_phone_tv);
        relieve_acount_code_et = (EditText) findViewById(R.id.relieve_acount_code_et);
        relieve_acount_code_tv = (TextView) findViewById(R.id.relieve_acount_code_tv);
        relieve_acount_code_tv.setOnClickListener(this);
        relieve_acount_config = (TextView) findViewById(R.id.relieve_acount_config);
        relieve_acount_config.setOnClickListener(this);


        phoneNum = MyApp.userData.getData().getPhone();
        relieve_acount_phone_tv.setText(phoneNum);
        type = getIntent().getStringExtra("type");

    }

    private void getCode(){
        Map<String,Object> map = new HashMap<>();
        map.put("phone",phoneNum);
        XUtil.Post(UrlConfig.GETCODE_URL,map,new MyCallBack<String>(){

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                //MyLogger.wLog().e("获取验证码result="+result);
                JSONObject jsonObject = JSONObject.parseObject(result);
                if (jsonObject.getInteger("code")==200){
                    startCountBack();
                }
                showToast(jsonObject.getString("msg"));
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                MyLogger.wLog().e("获取验证码失败");
            }
        });
    }

    /**
     * 解绑
     */
    private void relieveAcount(){
        String code = relieve_acount_code_et.getText().toString().trim();
        if (code.isEmpty()){
            showToast("请输入验证");
        }
        Map<String,Object> map = new HashMap<>();
        map.put("user",MyApp.userData.getData().getUser_id());
        map.put("token",MyApp.userData.getData().getToken());
        map.put("phone",phoneNum);
        map.put("code",code);
        map.put("type",type);
        //Log.e("info","type="+type);
        XUtil.Post(UrlConfig.RELIEVE_ACOUNT_URL,map,new MyCallBack<String>(){

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                //MyLogger.wLog().e("解绑result="+result);
                JSONObject jsonObject = JSONObject.parseObject(result);
                if (jsonObject.getInteger("code")==200){
                    if (type.equals("weipay")){
                        MyApp.userData.getData().getAccount_info().getWeipay().setState(0);
                    }else if (type.equals("alipay")){
                        MyApp.userData.getData().getAccount_info().getAlipay().setState(0);
                    }
                    RelieveAcountActivity.this.setResult(Constants.RESULT_DATA);
                    RelieveAcountActivity.this.finish();
                }
                showToast(jsonObject.getString("msg"));
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                MyLogger.wLog().e("解绑失败");
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.relieve_acount_back:
                finish();
                break;
            case R.id.relieve_acount_code_tv:
                getCode();
                break;
            case R.id.relieve_acount_config:
                relieveAcount();
                break;
        }
    }
}
